package com.bingxue.edu.management.system.service;

import com.bingxue.edu.management.system.mapper.EmployeeMapper;
import com.bingxue.edu.management.system.mapper.RoleEmployeeMapper;
import com.bingxue.edu.management.system.model.entity.RoleEmployee;
import com.bingxue.edu.management.system.model.req.RoleEmployeeReq;
import com.bingxue.edu.management.system.model.resp.EmployeeResp;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.mybatis.base.BaseService;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.system.model.entity.table.EmployeeTableDef.EMPLOYEE;
import static com.bingxue.edu.management.system.model.entity.table.RoleEmployeeTableDef.ROLE_EMPLOYEE;

/**
 * 角色-员工业务实现类
 *
 * @author panbo
 * @since 2024/08/21 13:18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleEmployeeService extends BaseService<RoleEmployeeMapper, RoleEmployee> {

    private final EmployeeMapper employeeMapper;

    /**
     * 根据角色id查询员工列表
     * @param roleId 角色id
     * @return
     */
    public Pagination<EmployeeResp> getEmployeePageByRoleId(String roleId, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(EMPLOYEE.ALL_COLUMNS)
                .from(ROLE_EMPLOYEE)
                .leftJoin(EMPLOYEE).on(ROLE_EMPLOYEE.EMPLOYEE_ID.eq(EMPLOYEE.ID))
                .where(ROLE_EMPLOYEE.ROLE_ID.eq(roleId));
        return Pagination.of(this.mapper.paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, EmployeeResp.class));
    }

    /**
     * 移除员工角色
     * @param req
     */
    public void removeRoleEmployee(RoleEmployeeReq req) {
        RoleEmployee roleEmployee = new RoleEmployee();
        roleEmployee.setRoleId(req.getRoleId());
        roleEmployee.setEmployeeId(req.getEmployeeId());

        this.remove(QueryWrapper.create(roleEmployee));
    }

    /**
     * 添加员工角色
     * @param req
     */
    public void createRoleEmployee(RoleEmployeeReq req) {
        RoleEmployee roleEmployee = new RoleEmployee();
        roleEmployee.setRoleId(req.getRoleId());
        roleEmployee.setEmployeeId(req.getEmployeeId());
        // 判断是否存在
        ValidationUtils.throwIf(isRoleEmployeeExists(roleEmployee), "员工已经绑定该角色");
        this.save(roleEmployee);
    }

    /**
     * 查询员工拥有角色id列表
     * @param employeeId 员工id
     * @return 角色id列表
     */
    public List<String> getRoleIdListByEmployeeId(String employeeId) {
        return this.queryChain()
                .select(ROLE_EMPLOYEE.ROLE_ID)
                .where(ROLE_EMPLOYEE.EMPLOYEE_ID.eq(employeeId))
                .listAs(String.class);
    }

    public boolean isRoleEmployeeExists(RoleEmployee roleEmployee) {
        return this.exists(QueryWrapper.create(roleEmployee));
    }
}
